package com.zy.recursion;

import com.zy.ListNode;

/**
 * @description: 两两交换链表中的节点
 * @author: zhaoyi
 * @create: 2019-08-28 23:08
 * https://leetcode-cn.com/explore/orignial/card/recursion-i/256/principle-of-recursion/1201/
 **/
public class TwoChange {

    // 给定 1->2->3->4, 你应该返回 2->1->4->3.
    public ListNode swapPairs(ListNode head) {

        if(head == null || head.next == null){
            return head;
        }

        if (head.next.next != null) {
            ListNode next = head.next;
            ListNode twoNext = next.next;
            head.next.next = head;
            head.next = swapPairs(twoNext);
            return next;
        } else {
            head.next.next = head;
            ListNode next = head.next;
            head.next = null;
            return next;
        }
    }
}
